<template>
    <el-dialog :model-value="show" title="添加用户" @close="emit('hide')">
        <el-form ref="formRef" :model="form" :rules="rules">
            <el-form-item label="账号" prop="username">
                <el-input v-model="form.username" autocomplete="off" />
            </el-form-item>
            <el-form-item label="姓名" prop="nickName">
                <el-input v-model="form.nickName" autocomplete="off" />
            </el-form-item>
            <el-form-item label="邮箱" prop="email">
                <el-input v-model="form.email" autocomplete="off" />
            </el-form-item>
            <el-form-item label="密码" prop="password">
                <el-input v-model="form.password" type="password" :disabled="id !== 0" autocomplete="off" />
            </el-form-item>
            <el-form-item label="备注" prop="note">
                <el-input v-model="form.note" :rows="4" type="textarea" autocomplete="off" />
            </el-form-item>
            <el-form-item label="是否启用">
                <el-radio-group v-model="form.status" class="ml-4">
                    <el-radio :label="1">是</el-radio>
                    <el-radio :label="0">否</el-radio>
                </el-radio-group>
            </el-form-item>
        </el-form>
        <template #footer>
            <span class="dialog-footer">
                <el-button @click="cancelFn">取消</el-button>
                <el-button type="primary" @click="submitFn">确定</el-button>
            </span>
        </template>
    </el-dialog>
</template>

<script setup lang='ts'>
import { reactive, ref, watch } from "vue";
import { FormInstance, ElMessage, ElMessageBox } from "element-plus";

import { addUserAPI, getUserByIdAPI, upDateUserAPI } from "./api";
const props = defineProps({
    show: {
        type: Boolean,
        default: false,
    },
    id: {
        type: Number,
        default: 0,
    }
});

const emit = defineEmits(["hide"]);

const form = reactive<IList>({
    nickName: "",
    username: "",
    password: "",
    note: "",
    status: 0,
    email: "",
});

const validateEmail = (rule: any, value: string, cb: any) => {
    if (!/^\w+[@]\w+[.]\w+/.test(value)) {
        return cb(new Error("邮箱格式不正确"));
    } else {
        cb();
    }
};

const formRef = ref<FormInstance>();

const rules = {
    username: [{ required: true, trigger: "blur", message: "请填写用账号" }],
    email: [{ validator: validateEmail, trigger: "blur" }],
    password: [{ required: true, trigger: "blur", message: "请填写密码" }],
    nickName: [{ required: true, trigger: "blur", message: "请填写用户名" }],
    note: [{ required: false, trigger: "blur", message: "请填写用户名" }],
};


const cancelFn = () => {
    emit("hide");
    formRef.value?.resetFields()
}

watch(() => props.id, () => {
    if (props.id !== 0) {
        getUserByIdAPI(props.id).then(res => {
            if (res.code === 200) {
                Object.assign(form, res.data)
            }
        })
    }
})

const addUser = () => {
    // 二次确认
    ElMessageBox.confirm("确定要添加此用户吗？", "添加用户", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
    })
        .then(() => {
            addUserAPI(form).then((res) => {
                if (res.code === 200) {
                    ElMessage.success("添加成功")
                    emit("hide", true);
                    formRef.value?.resetFields();
                }
            });
        })
        .catch(() => {
            console.log("点击的取消");
        });
}

const editUser = () => {
    ElMessageBox.confirm("确定要修改此用户吗？", "添加用户", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
    }).then(() => {
        upDateUserAPI(form).then(res => {
            if (res.code === 200) {
                ElMessage.success("修改成功")
                formRef.value?.resetFields();
                emit('hide', true)
            }
        })
    }).catch(() => {

    })
}



const submitFn = () => {
    // 再一次数据的校验
    formRef.value?.validate((ok: boolean) => {
        if (ok) {
            if (props.id === 0) {
                addUser()
            } else {
                editUser()
            }
        } else {
            // 提示
        }
    });
};
</script>
<style lang="less" scoped>

</style>